﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMembership;
using NapCardOnline.Models;

namespace NapCardOnline.Controllers
{ 
    public class NewsController : Controller
    {
        private NapCardOnlineEntities db = new NapCardOnlineEntities();

        //
        // GET: /News/

        public ViewResult Index()
        {
            var news = db.News.Include("CategoryNew").Include("Language");
            return View(news.ToList());
        }

        //
        // GET: /News/Details/5

        public ViewResult Details(int id)
        {
            News news = db.News.Single(n => n.NewsID == id);
            return View(news);
        }

        //
        // GET: /News/Create

        public ActionResult Create()
        {
            ViewBag.FK_CategoryNewsID = new SelectList(db.CategoryNews, "CategoryNewsID", "CategoryNewsNo");
            ViewBag.FK_LanguageID = new SelectList(db.Languages, "LanguageID", "LanguageNo");
            return View();
        } 

        //
        // POST: /News/Create

        [HttpPost]
        public ActionResult Create(News news)
        {
            if (ModelState.IsValid)
            {
                db.News.AddObject(news);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.FK_CategoryNewsID = new SelectList(db.CategoryNews, "CategoryNewsID", "CategoryNewsNo", news.FK_CategoryNewsID);
            ViewBag.FK_LanguageID = new SelectList(db.Languages, "LanguageID", "LanguageNo", news.FK_LanguageID);
            return View(news);
        }
        
        //
        // GET: /News/Edit/5
 
        public ActionResult Edit(int id)
        {
            News news = db.News.Single(n => n.NewsID == id);
            ViewBag.FK_CategoryNewsID = new SelectList(db.CategoryNews, "CategoryNewsID", "CategoryNewsNo", news.FK_CategoryNewsID);
            ViewBag.FK_LanguageID = new SelectList(db.Languages, "LanguageID", "LanguageNo", news.FK_LanguageID);
            return View(news);
        }

        //
        // POST: /News/Edit/5

        [HttpPost]
        public ActionResult Edit(News news)
        {
            if (ModelState.IsValid)
            {
                db.News.Attach(news);
                db.ObjectStateManager.ChangeObjectState(news, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.FK_CategoryNewsID = new SelectList(db.CategoryNews, "CategoryNewsID", "CategoryNewsNo", news.FK_CategoryNewsID);
            ViewBag.FK_LanguageID = new SelectList(db.Languages, "LanguageID", "LanguageNo", news.FK_LanguageID);
            return View(news);
        }

        //
        // GET: /News/Delete/5
 
        public ActionResult Delete(int id)
        {
            News news = db.News.Single(n => n.NewsID == id);
            return View(news);
        }

        //
        // POST: /News/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            News news = db.News.Single(n => n.NewsID == id);
            db.News.DeleteObject(news);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}